package com.mumu.android.tcp;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.text.Html;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

/**
 * @Author: MuMu
 * @Date: 2024/12/11 10:33
 */
public abstract class BaseActivity extends AppCompatActivity {
    protected Context context = this;
    private ImageView toBack = null, btnTopSetting = null;
    private TextView topTitle = null;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
    }

    protected void setTitle(String text){
        if (topTitle==null) topTitle = findViewById(R.id.topTitle);
        if (topTitle!=null) topTitle.setText(text);
    }



    protected void toPage(Class<?> c, boolean finish){
        startActivity(new Intent(context, c));
        if (finish) finish();
    }

    protected void setToBack(){
        if (toBack==null) toBack = findViewById(R.id.toBack);
        if (toBack==null) return;
        toBack.setVisibility(View.VISIBLE);
        toBack.setOnClickListener(view -> {
            onBackPressed();
            finish();
        });
    }

    protected void setToBack(View.OnClickListener listener){
        if (toBack==null) toBack = findViewById(R.id.toBack);
        if (toBack==null) return;
        toBack.setVisibility(View.VISIBLE);
        toBack.setOnClickListener(listener);
    }

    protected void hindBack(){
        if (toBack==null) toBack = findViewById(R.id.toBack);
        if (toBack==null) return;
        toBack.setVisibility(View.GONE);
    }


    protected void setBtnTopSetting(View.OnClickListener listener){
        if (btnTopSetting==null) btnTopSetting = findViewById(R.id.btnTopSetting);
        if (btnTopSetting==null) return;
        btnTopSetting.setVisibility(View.VISIBLE);
        btnTopSetting.setOnClickListener(listener);
    }

    protected void  showToast(String text){
        runOnUiThread(()->{
            View customToastView = getLayoutInflater().inflate(R.layout.custom_toast_layout, null);
            TextView textView = customToastView.findViewById(R.id.text);
            Toast customToast = new Toast(getApplicationContext());
            textView.setText(text);
            customToast.setView(customToastView);
            customToast.setDuration(Toast.LENGTH_SHORT);
            customToast.setGravity(Gravity.CENTER , 0, 50);
            customToast.show();
        });
    }


    protected void setLog(TextView textView, String log, LogType type, boolean clear){
        String color = "black";
        switch (type) {
            case DEFAULT:
                color = "#0BE26D"; break;
            case SUCCEED:
                color = "#2A5EFF"; break;
            case ERROR:
                color = "#FF1212"; break;
            case WARNING:
                color = "#F6B91D"; break;
        }
        String msgFormat = "<font color='tCOLOR'>TEXT</font>".replace("tCOLOR", color)
                .replace("TEXT", log);

        runOnUiThread(()->{
            if (!clear){
                if (!textView.getText().toString().isEmpty()){
                    textView.append("\n");
                }
                textView.append(Html.fromHtml(msgFormat));
            } else {
                textView.setText("");
                textView.append(Html.fromHtml(msgFormat));
            }
        });

    }

    protected enum LogType{
        DEFAULT, WARNING, ERROR, SUCCEED
    }


    }
